package com.code.origin.configurations;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import java.util.ArrayList;

/**
 * Swagger2配置类
 * @author: Huangds
 * @date: 2020-12-23 15:37
 * @version: v1.0
 */
@Configuration
@ConditionalOnProperty(prefix = "swagger", value = {"enable"}, havingValue = "true")
@EnableSwagger2
public class Swagger2Config {

    @Bean
    public Docket call9mePubApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("CALL9ME-API")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.call9me.page"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket call9meCommonApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("COMMON-API")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.call9me.api.common"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket call9meBusinessAppApi() {
        //添加通用head参数
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameterList = new ArrayList<>();
        parameterBuilder.name("access_token").description("授权TOKEN").modelRef(new ModelRef("string")).parameterType("query").required(false).defaultValue("").build();
        parameterList.add(parameterBuilder.build());
        parameterBuilder.name("BUSINESS_TOKEN").description("用户TOKEN").modelRef(new ModelRef("string")).parameterType("header").required(false).defaultValue("").build();
        parameterList.add(parameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(parameterList)
                .groupName("BUSINESS-API")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.call9me.api.business"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket call9meIodAppApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("SHOP-API")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.call9me.api.shop.v1"))
                .paths(PathSelectors.any())
                .build();
    }

    @Bean
    public Docket call9meMobileApi() {
        //添加通用head参数
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        List<Parameter> parameterList = new ArrayList<>();
        parameterBuilder.name("token").description("授权码").modelRef(new ModelRef("string")).parameterType("header").required(false).defaultValue("").build();
        parameterList.add(parameterBuilder.build());

        return new Docket(DocumentationType.SWAGGER_2)
                .globalOperationParameters(parameterList)
                .groupName("MOBILE-API")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.call9me.api.shop.mobile"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 酒友圈接口
     * @author liangyun
     * @since 2018-08-01
     */
    @Bean
    public Docket call9meJYQApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("JYQ-API")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.call9me.api.jyq"))
                .paths(PathSelectors.any())
                .build();
    }

    /**
     * 客服7陌接口
     * @author liangyun
     * @since 2018-11-01
     */
    @Bean
    public Docket call9meKFM7Api() {
        return new Docket(DocumentationType.SWAGGER_2)
                .groupName("KF-M7-API")
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.call9me.api.m7"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Call9me")
                .description("Call9me APIS")
                .contact("@JiaoJiu Technical Department")
                .version("1.0.0")
                .build();
    }

}

